﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;


namespace Eu.Xquirrel.Networking.Http.Offline
{
    public class MimeTypes
    {
        private static readonly StorageFolder LocalFolder
             = Windows.ApplicationModel.Package.Current.InstalledLocation;

        private static Dictionary<string, string> _mimes = new Dictionary<string, string>();

        public MimeTypes()
        {
        }

        public async Task<Dictionary<string, string>> GetMimeList()
        {

            var uri = new System.Uri("ms-appx:///Eu.Xquirrel.Networking.Http.Offline/mime.txt");
            var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);

            var readFile = await Windows.Storage.FileIO.ReadTextAsync(file);
            string[] lines = readFile.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            string[] a = lines;
            for (int i = 0; i < lines.Count(); i++ )
            {
                try
                {
                    string ext = SplitWhitespace(lines[i])[0];
                    if (!_mimes.ContainsKey(ext))
                    {
                        _mimes.Add(SplitWhitespace(lines[i])[0], SplitWhitespace(lines[i])[1]);
                    }
                }
                catch (Exception ex)
                {

                }
                Debug.WriteLine(SplitWhitespace(lines[i])[0]);
            }
            return _mimes;
        }

        public Dictionary<string, string> MimeDictionary
        {
            get { return _mimes; }
        }

        private static string[] SplitWhitespace(string input)
        {
            char[] whitespace = new char[] { ' ', '\t' };
            return input.Split(whitespace);
        }

    }
}

